using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace ICeFramework.Win.UI
{
    public partial class GradientLabel : Label
    {
        public enum GradientSpecification { gsText, gsBackground };

        #region Implementtation Member Fields
        protected Color gradientColorOne = Color.White;
        protected Color gradientColorTwo = Color.Blue;
        protected Color fixedColor = Color.Black;
        protected Color marginColor = Color.Black;
        protected LinearGradientMode lgm = LinearGradientMode.ForwardDiagonal;
        protected Border3DStyle b3dstyle = Border3DStyle.Bump;
        protected GradientSpecification draw_style = GradientSpecification.gsBackground;
        protected float margin = 1;
        protected bool border = true;
        #endregion

        public GradientLabel()
        {
            InitializeComponent();
        }


		#region GradientColorOne Properties
		[
		DefaultValue(typeof(Color),"White"),
		Description("The first gradient color."),
		Category("Appearance"),
		]

		//GradientColorOne Properties
		public Color GradientColorOne
		{
			get 
			{
				return gradientColorOne;
            }
			set
			{
				gradientColorOne = value;
				Invalidate();
			}
		}
		#endregion
		
		#region GradientColorTwo Properties
		[
		DefaultValue(typeof(Color),"Blue"),
		Description("The second gradient color."),
		Category("Appearance"),
		]

		//GradientColorTwo Properties
		public Color GradientColorTwo
		{
			get 
			{
				return gradientColorTwo;
			}
			set
			{
				gradientColorTwo = value;
				Invalidate();
			}
		}

		#endregion

		#region LinearGradientMode Properties
		//LinearGradientMode Properties
		[
		DefaultValue(typeof(LinearGradientMode),"ForwardDiagonal"),
		Description("Gradient Mode"),
		Category("Appearance"),
		]
		
		public LinearGradientMode GradientMode
		{
			get 
			{
				return lgm;
			}
			
			set
			{
				lgm = value;
				Invalidate();
			}
		}
		#endregion
        
		#region Border3DStyle Properties
		//Border3DStyle Properties
		[
		DefaultValue(typeof(Border3DStyle),"Bump"),
		Description("BorderStyle"),
		Category("Appearance"),
		]

		// hide BorderStyle inherited from the base class
		new public Border3DStyle BorderStyle
		{
			get
			{
				return b3dstyle;
			}
			set
			{
				b3dstyle = value;
				Invalidate();
			}
		}
        [
        DefaultValue(typeof(Boolean), "True"),
        Description("Use Borders"),
        Category("Appearance"),
        ]
        public Boolean UseBorders
        {
            get
            {
                return border;
            }
            set
            {
                border = value;
                Invalidate();
            }
        }
        #endregion

        #region DrawMode Properties
        [
        DefaultValue(typeof(GradientLabel.GradientSpecification), "Bump"),
        Description("DrawStyle"),
        Category("Appearance"),
        ]
        public GradientSpecification DrawStyle
        {
            get
            {
                return draw_style;
            }
            set
            {
                draw_style = value;
                Invalidate();
            }
        }

        [
        DefaultValue(typeof(Color), "Black"),
        Description("The color for non gradient drawing."),
        Category("Appearance"),
        ]
        public Color FixedColor
        {
            get
            {
                return fixedColor;
            }
            set
            {
                fixedColor = value;
                Invalidate();
            }
        }

        [
        DefaultValue(typeof(Color), "Black"),
        Description("The color for text margins."),
        Category("Appearance"),
        ]
        public Color MarginColor
        {
            get
            {
                return marginColor;
            }
            set
            {
                marginColor = value;
                Invalidate();
            }
        }
        [
        DefaultValue(typeof(Int32), "Black"),
        Description("The tickness for margins."),
        Category("Appearance"),
        ]
        public float MarginTickness
        {
            get
            {
                return margin;
            }
            set
            {
                margin = value;
                Invalidate();
            }
        }
        #endregion

        #region Removed Properties

        // Remove BackColor Property
		[
		Browsable(false),
		EditorBrowsable(EditorBrowsableState.Never)
		]
		public override System.Drawing.Color BackColor
		{
			get	
			{
				return new System.Drawing.Color();
			}
			set	{;}
		}
        // Remove ForeColor Property
        [
        Browsable(false),
        EditorBrowsable(EditorBrowsableState.Never)
        ]
        public override System.Drawing.Color ForeColor
        {
            get
            {
                return new System.Drawing.Color();
            }
            set { ;}
        }
		
		#endregion


		protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
		{
		}
        
		protected override void OnPaintBackground(System.Windows.Forms.PaintEventArgs pevent)
        {
            //System.Threading.ReaderWriterLock a = new System.Threading.ReaderWriterLock();
            
			Graphics gfx = pevent.Graphics;
            gfx.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            Rectangle rect = new Rectangle(-1, -1, this.Width + 1, this.Height + 1);
            SolidBrush b = new SolidBrush(fixedColor);
            LinearGradientBrush lgb = new LinearGradientBrush(rect, gradientColorOne, gradientColorTwo, lgm);
            SizeF size_str = gfx.MeasureString(this.Text, this.Font);
            GraphicsPath fontpath = new GraphicsPath();
            fontpath.AddString(this.Text, this.Font.FontFamily, (int)this.Font.Style, gfx.DpiX * this.Font.SizeInPoints / 72, new Rectangle(10, -1, this.Width + 1, this.Height + 1), StringFormat.GenericDefault);
            if (draw_style == GradientSpecification.gsBackground)
            {
                gfx.FillRectangle(lgb, rect);
                gfx.FillPath(b, fontpath);
            }
            else
            {
                gfx.FillRectangle(b, rect);
                gfx.FillPath(lgb, fontpath);
            }
            if (margin > 0)
            {
                Pen p = new Pen(marginColor, margin);
                gfx.DrawPath(p, fontpath);
                p.Dispose();
            }
            if (border)
            {
                rect.Inflate(-2, -2);
                ControlPaint.DrawBorder3D(gfx, rect, b3dstyle);
            }
            LinearGradientBrush lgbl = new LinearGradientBrush(rect, gradientColorOne, gradientColorTwo, LinearGradientMode.Horizontal);
            Pen pn = new Pen(lgbl, 2);
            gfx.DrawLine(pn, new Point(0, (int)size_str.Height / 2), new Point(10, (int)size_str.Height - 3));
            gfx.DrawLine(pn, new Point(10, (int)size_str.Height - 3), new Point(Width, (int)size_str.Height - 3));

            lgb.Dispose();
            lgbl.Dispose();
            fontpath.Dispose();
            pn.Dispose();
            b.Dispose();
            lgb.Dispose();
		}

    }
}